package cn.wolfcode.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    IRoleService roleService;
    @Autowired
    IPermissionService permissionService;
@RequestMapping("/list")
    public String list(QueryObject qo, Model model){
        PageInfo pageInfo = roleService.query(qo);
        model.addAttribute("PageInfo",pageInfo);
        return "role/list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);

        if (id!=null){

          Role role = roleService.selectById(id);
          model.addAttribute("role",role);
            List list = roleService.selectByRoleId(id);
            model.addAttribute("selfPermission",list);
            System.out.println(list);
      }
      return "role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){

    if (role.getId()!=null){
       roleService.update(role,ids);

    }   else{
        roleService.save(role,ids);

    }
    return "redirect:/role/list";
  }
  @RequestMapping("/delete")
  public String delete(Long id){
    roleService.delete(id);
    return "redirect:/role/list";
  }


 }
